package com.bookcode.controller;
import com.bookcode.entity.Student;
import com.bookcode.service.StudentService;
import com.bookcode.entity.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class StudentController {

    @Autowired
    StudentService studentService;
    //增加学生
//    @PostMapping(value = "/students",params = {"name"})
    @RequestMapping(value = "/addStudent",method=RequestMethod.POST)
    public HttpResult addStudent( Student student) {
        System.out.println(student);
        HttpResult result = new HttpResult();
        try {
            studentService.save(student);
            result.setStatus("ok");
        }catch (Exception e){
            result.setStatus("wrong");
        }finally {
            return result;
        }

    }

    /**
     *
     *删除学生
     */
    @GetMapping(value = "/delete/xuehao/{xuehao}")
    public HttpResult deleteStudentByXuehao(@PathVariable("xuehao") String xuehao){
        HttpResult result=new HttpResult();
        try{
            studentService.deleteStudentByXuehao(xuehao);
            result.setData(xuehao);
            result.setStatus("delete Ok");
        }catch (Exception e){
            result.setStatus("delete fail");
        }finally {
            return result;
        }
    }
    /**
     *
     *根据id删除学生
     */
    @GetMapping(value = "/deleteStudentById/{id}")
    public HttpResult deleteStudentById(@PathVariable("id") Long id){
        HttpResult result=new HttpResult();
        try{
            studentService.deleteStudentById(id);
            result.setData("delete:"+id);
            result.setStatus("delete Ok");
        }catch (Exception e){
            result.setStatus("delete fail");
        }finally {
            return result;
        }
    }

    //根据id查找学生
    @GetMapping(value = "/student/id/{id}")
    public HttpResult queryStudentById(@PathVariable Long id){
        HttpResult<Student> result = new HttpResult<>();
        result.setData(studentService.findStudentById(id));
        result.setStatus("ok");
        return result;
    }
    //查找所有学生
    @GetMapping(value = "/students")
    public HttpResult queryAll(){
        HttpResult<List<Student>> result = new HttpResult<>();
        result.setData(studentService.findAll());
        result.setStatus("ok");
        return result;
    }
    //根据学号查找学生
    @GetMapping(value = "/student/xuehao/{xuehao}")
    public HttpResult queryStudentByXuehao(@PathVariable String  xuehao){
        HttpResult<Student> result = new HttpResult<>();
        result.setData(studentService.findStudentByXuehao(xuehao));
        result.setStatus("ok");
        return result;
    }
    //根据学号查找学生
    @GetMapping(value = "/student/username/{username}")
    public HttpResult queryStudentByUsername(@PathVariable String  username){
        HttpResult<Student> result = new HttpResult<>();
        result.setData(studentService.findStudentByUsername(username));
        result.setStatus("ok");
        return result;
    }

}
